<?php
/**
 * Roots includes
 *
 * The $roots_includes array determines the code library included in your theme.
 * Add or remove files to the array as needed. Supports child theme overrides.
 *
 * Please note that missing files will produce a fatal error.
 *
 * @link https://github.com/roots/roots/pull/1042
 */
$roots_includes = array(
  'lib/utils.php',           // Utility functions
  'lib/init.php',            // Initial theme setup and constants
  'lib/wrapper.php',         // Theme wrapper class
  'lib/sidebar.php',         // Sidebar class
  'lib/config.php',          // Configuration
  'lib/activation.php',      // Theme activation
  'lib/titles.php',          // Page titles
  'lib/nav.php',             // Custom nav modifications
  'lib/gallery.php',         // Custom [gallery] modifications
  'lib/comments.php',        // Custom comments modifications
  'lib/scripts.php',         // Scripts and stylesheets
  'lib/extras.php',          // Custom functions
);

foreach ($roots_includes as $file) {
  if (!$filepath = locate_template($file)) {
    trigger_error(sprintf(__('Error locating %s for inclusion', 'roots'), $file), E_USER_ERROR);
  }

  require_once $filepath;
}
unset($file, $filepath);

/** registration Asahi Kasei styles & scripts */
function asahi_kasei_styles()
{
  // Add file bootstrap.min.css 
  wp_enqueue_style( 'bootstrap.min', get_template_directory_uri() . '/assets/css/bootstrap.min.css');
  // Add file override.css 
  wp_enqueue_style( 'override', get_template_directory_uri() . '/assets/css/override.css');
  //add file jquery.min.js
  wp_enqueue_script( 'jquery-min', get_template_directory_uri() . '/assets/js/jquery.min.js', array( ), '');
  //add file bootraps.min.js
  wp_enqueue_script( 'bootraps-min', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array(), '');
  //add file functions.js
  wp_enqueue_script( 'functions', get_template_directory_uri() . '/assets/js/functions.js', array(), '');
}
add_action('wp_head','asahi_kasei_styles');

/** Implement Custom Header features. */
require get_template_directory() . '/custom-header.php';

/** AsahiKasei breadcrumb */
function asahi_kasei_breadcrumb() {

  $delimiter = '&raquo;';

  $home = get_bloginfo('name');

  $before = '';

  $after = '</li>';

  global $post;
  
  $homeLink = get_bloginfo('url');
  
  echo '<ol class="breadcrumb"><li><a href="' . $homeLink . '">' . 'トップページ' . '</a> ' . '</li>';
  
  if ( is_category() ) {
  
    global $wp_query;
    
    $cat_obj = $wp_query->get_queried_object();
    
    $thisCat = $cat_obj->term_id;
    
    $thisCat = get_category($thisCat);
    
    $parentCat = get_category($thisCat->parent);
    
    if ($thisCat->parent != 0) echo('<li>'.get_category_parents($parentCat, TRUE,'</li><li>'));
    
    echo $before . single_cat_title('', false) . $after;
  
  } elseif ( is_day() ) {
  
    echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
    
    echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
    
    echo $before . 'Archive by date "' . get_the_time('d') . '"' . $after;
  
  } elseif ( is_month() ) {
  
    echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
    
    echo $before . 'Archive by month "' . get_the_time('F') . '"' . $after;
  
  } elseif ( is_year() ) {
  
    echo $before . 'Archive by year "' . get_the_time('Y') . '"' . $after;
  
  } elseif ( is_single() && !is_attachment() ) {
  
    if ( get_post_type() != 'post' ) {
    
      $post_type = get_post_type_object(get_post_type());
      
      $slug = $post_type->rewrite;
      
      echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>' . $delimiter . ' ';
      
      echo $before . get_the_title() . $after;
    
    } else {
    
      $cat = get_the_category(); $cat = $cat[0];
      
      echo '<li>' . get_category_parents($cat, TRUE, '</li><li>') . ' ';
      
      echo $before . get_the_title() . $after;
    
    }
  
  } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  
    $post_type = get_post_type_object(get_post_type());
  
    echo $before . $post_type->labels->singular_name . $after;
  
  } elseif ( is_attachment() ) {
  
    $parent_id  = $post->post_parent;
    
    $breadcrumbs = array();
    
    while ($parent_id) {
    
      $page = get_page($parent_id);
      
      $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
      
      $parent_id    = $page->post_parent;
    
    }
    
    $breadcrumbs = array_reverse($breadcrumbs);
    
    foreach ($breadcrumbs as $crumb) echo ' ' . $crumb . ' ' . $delimiter . ' ';

    echo $before . 'You&apos;re currently viewing "' . get_the_title() . '"' . $after;
  
  } elseif ( is_page() && !$post->post_parent ) {
  
    echo $before . ' ' . get_the_title() . $after;
  
  } elseif ( is_page() && $post->post_parent ) {
  
    $parent_id  = $post->post_parent;
    
    $breadcrumbs = array();

    
    while ($parent_id) {
    
      $page = get_page($parent_id);
      
      $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
      
      $parent_id    = $page->post_parent;
    
    }
    
    $breadcrumbs = array_reverse($breadcrumbs);
    
    foreach ($breadcrumbs as $crumb) echo ' ' . $crumb . ' ' . $delimiter . ' ';
    
    echo $before . 'You&apos;re currently reading "' . get_the_title() . '"' . $after;
  
  } elseif ( is_search() ) {
  
    echo $before . 'Search results for "' . get_search_query() . '"' . $after;
  
  } elseif ( is_tag() ) {
  
    echo $before . 'Archive by tag "' . single_tag_title('', false) . '"' . $after;
  
  } elseif ( is_author() ) {
  
    global $author;
    
    $userdata = get_userdata($author);
    
    echo $before . 'Articles posted by "' . $userdata->display_name . '"' . $after;
  
  } elseif ( is_404() ) {
  
    echo $before . 'You got it "' . 'Error 404 not Found' . '"&nbsp;' . $after;
  
  }

  if ( get_query_var('paged') ) {
  
    if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  
    echo ('Page') . ' ' . get_query_var('paged');
  
    if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  
  }
  
  echo '</ol>';
}

/** Pagination */
function wpbeginner_numeric_posts_nav() {

	if( is_singular() )
		return;

	global $wp_query;

	/** Stop execution if there's only 1 page */
	if( $wp_query->max_num_pages <= 1 )
		return;

	$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
	$max   = intval( $wp_query->max_num_pages );

	/**	Add current page to the array */
	if ( $paged >= 1 )
		$links[] = $paged;

	/**	Add the pages around the current page to the array */
	if ( $paged >= 3 ) {
		$links[] = $paged - 1;
		$links[] = $paged - 2;
	}

	if ( ( $paged + 2 ) <= $max ) {
		$links[] = $paged + 2;
		$links[] = $paged + 1;
	}

	echo '<div class="pagination"><ul>' . "\n";

	/**	Previous Post Link */
	if ( get_previous_posts_link() )
		printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

	/**	Link to first page, plus ellipses if necessary */
	if ( ! in_array( 1, $links ) ) {
		$class = 1 == $paged ? ' class="active"' : '';

		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

		if ( ! in_array( 2, $links ) )
			echo '<li>…</li>';
	}

	/**	Link to current page, plus 2 pages in either direction if necessary */
	sort( $links );
	foreach ( (array) $links as $link ) {
		$class = $paged == $link ? ' class="active"' : '';
		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
	}

	/**	Link to last page, plus ellipses if necessary */
	if ( ! in_array( $max, $links ) ) {
		if ( ! in_array( $max - 1, $links ) )
			echo '<li>…</li>' . "\n";

		$class = $paged == $max ? ' class="active"' : '';
		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
	}

	/**	Next Post Link */
	if ( get_next_posts_link() )
		printf( '<li>%s</li>' . "\n", get_next_posts_link() );

	echo '</ul></div>' . "\n";

}
